package com.example.myapplication.activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.myapplication.BaseActivity;
import com.example.myapplication.R;
import com.example.myapplication.database.User;
import com.example.myapplication.util.NameUtil;
import com.example.myapplication.util.ToastUtil;

import org.litepal.LitePal;

import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class RegisActivity extends BaseActivity {

    private EditText etPhone,etPwd1,etPwd2,etSms;
    private TextView btSendSms,btRegis;
    private CompositeDisposable cd;
    private int countDownSec=60;
    private boolean isChange=false;
    @Override
    protected void initView() {
        btRegis=findViewById(R.id.btRegis);
        btSendSms=findViewById(R.id.btSendSms);

        etPhone=findViewById(R.id.etPhone);
        etSms=findViewById(R.id.etSms);
        etPwd1=findViewById(R.id.etPwd1);
        etPwd2=findViewById(R.id.etPwd2);

        cd=new CompositeDisposable();
    }

    @Override
    protected void logicStart() {
        isChange = getIntent().getBooleanExtra("isChange", false);

        if (isChange){
            btRegis.setText("修改密码");
        }else {
            btRegis.setText("立即注册");
        }

    }

    @Override
    protected void setListener() {
        btSendSms.setOnClickListener(v -> {
            cd.clear();
            Disposable subscribe = Observable.intervalRange(0, countDownSec, 0, 1, TimeUnit.SECONDS)
                    .doOnSubscribe(disposable -> cd.add(disposable))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())

                    .subscribe(new Consumer<Long>() {
                        @SuppressLint("SetTextI18n")
                        @Override
                        public void accept(Long aLong) throws Exception {
                            if (aLong<60){
                                btSendSms.setText((countDownSec-aLong)+"s后重新发送");
                                btSendSms.setTextColor(Color.parseColor("#aaaaaa"));
                                btSendSms.setClickable(false);
                            }else {
                                btSendSms.setText("发送验证码");
                                btSendSms.setClickable(true);
                                btSendSms.setTextColor(Color.parseColor("#D5B200"));
                            }
                        }
                    });

        });
        btRegis.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (etPhone.getText().toString().isEmpty()){
                    ToastUtil.toast("请输入手机号");
                    return;
                }
                if (!etSms.getText().toString().equals("123321")){
                    ToastUtil.toast("验证码不正确");
                    return;
                }
                if (!etPwd1.getText().toString().equals(etPwd2.getText().toString())){
                    ToastUtil.toast("两次输入的密码不一致，请重新输入");
                    return;
                }

                if (isChange){
                    List<User> userList = LitePal.where("phone = '" + etPhone.getText().toString() + "'").find(User.class);
                    if (userList.isEmpty()){
                        ToastUtil.toast("没有该手机号哦~,请您重新注册");
                        return;
                    }else {
                        userList.get(0).setPwd(etPwd1.getText().toString());
                        userList.get(0).save();
                        ToastUtil.toast("修改成功！登录吧！");
                        finish();
                    }
                }else {
                    User user = new User();
                    user.setPhone(etPhone.getText().toString());
                    user.setPwd(etPwd1.getText().toString());
                    user.setName(NameUtil.randomName(true,3));
                    user.save();
                    ToastUtil.toast("注册成功，登录吧！");
                    finish();
                }


            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cd.dispose();
    }

    @Override
    public int getLayoutId() {
        return R.layout.aty_regis;
    }

}